package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时保存口味数据
     * @param dishDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到菜品表
        this.save(dishDto);

        //获取菜品口味
        List<DishFlavor> flavors = getDishFlavors(dishDto);
        //保存菜品口味数据
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish, dishDto);

        //查询菜品对应口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品基本信息到菜品表
        this.updateById(dishDto);

        //清理菜品对应口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //获取菜品口味
        List<DishFlavor> flavors = getDishFlavors(dishDto);
        //保存菜品口味数据
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 更改停售在售状态
     * @param status
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeStatus(Integer status, List<Long> ids) {
        //1. 查询ids对应dish
            //1.1 设置条件过滤器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);

            //1.2 查询
        List<Dish> list = this.list(queryWrapper);

        //2. 修改状态
        list = list.stream().map((item)->{
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());

        //3. 批量修改
        this.updateBatchById(list);
    }

    /**
     * 删除菜品信息，同时删除口味表对应信息
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteWithDishFlavor(List<Long> ids) {
        //1. 构造条件构造对象，查询setmeal_dish表中是否有该菜品
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);

            //1.1 统计dishId在setmeal_dish表中个数
        int setCount = setmealDishService.count(setmealDishLambdaQueryWrapper);
        if(setCount > 0){
            //有套餐含有该数据，抛出异常
            throw new CustomException("有菜品被套餐使用中，无法删除");
        }

        //2. 删除dish表中对应菜品
            //2.1 查询是否有菜品在售
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);

        int dishCount = this.count(dishLambdaQueryWrapper);
        if(dishCount > 0){
            //有菜品在售，抛出异常
            throw new CustomException("菜品售卖中，无法删除");
        }

        this.removeByIds(ids);

        //3. 删除dish_flavor表中对应口味数据
        LambdaQueryWrapper<DishFlavor> dFLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dFLambdaQueryWrapper.in(DishFlavor::getDishId,ids);

        dishFlavorService.remove(dFLambdaQueryWrapper);
    }

    /**
     * 将菜品id放入菜品口味表种
     * @param dishDto
     * @return
     */
    private static List<DishFlavor> getDishFlavors(DishDto dishDto) {
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        return flavors;
    }
}
